import json

from data_define import Record
class FileReader:
    def read_data(self)->list[Record]:
        pass


class TextFileReader(FileReader):
    def __init__(self,path):
        self.path = path

    def read_data(self) ->list[Record]:
        f = open(self.path,"r",encoding="utf-8")
        record_list:list[Record] = []
        file_list = f.readlines()
        for line in file_list:
            l = line.strip()
            date_list = l.split(",")
            record = Record(date_list[0],date_list[1],int(date_list[2]),date_list[3])
            record_list.append(record)
        f.close()
        return record_list


class JsonFileReader(FileReader):
    def __init__(self,path):
        self.path = path

    def read_data(self) ->list[Record]:
        f = open(self.path,"r",encoding="utf-8")
        record_list:list[Record] = []
        file_list = f.readlines()
        for line in file_list:
            data_list = json.loads(line)
            record = Record(data_list["date"],data_list["order_id"],int(data_list["money"]),
                            data_list["province"])
            record_list.append(record)
        f.close()
        return record_list



if __name__ == '__main__':
    text_reader = TextFileReader("D:/date.txt")
    record_list = text_reader.read_data()

    json_reader = JsonFileReader("D:/date1.txt")
    for x in record_list:
        print(x)

    for x in json_reader.read_data():
        print(x)

